

import {AzraelFrameWorkComponent} from "./AzraelFrameWork";
import GameFrameworkEntry from "../../src/Base/GameFrameworkEntry";
import GameFrameworkLog from "../../src/Base/Log/GameFrameworkLog";
import {ILogHelper} from "../../src/Base/Log/ILogHelper";
const {ccclass,property} = cc._decorator;

@ccclass
export class BaseComponent extends AzraelFrameWorkComponent {

    private defaultDpi: number = 96;  // default windows dpi
    private gameSpeedBeforePause: number = 1;
    private _realTime:number = 0;
    @property({
        displayName: "Resources资源加载"
    })
    private editorResourceMode: boolean = true;

    @property
    private defaultLogHelperName:string = "DefaultLogHelper";

    @property({
        type: cc.Integer,
        displayName: "游戏帧率"
    })
    private frameRate: number = 30;
    @property({
        type: cc.Float,
        displayName: "游戏速度"
    })
    private gameSpeed: number = 1;


    get EditorResourceMode() {
        return this.editorResourceMode;
    }

    set EditorResourceMode(value) {
        this.editorResourceMode = value;
    }

    get FrameRate(): number {
        return this.frameRate;
    }

    set FrameRate(value: number) {
        this.frameRate = value;
        cc.game.setFrameRate(this.frameRate);
    }

    get GameSpeed() {
        return this.gameSpeed;
    }

    set GameSpeed(value) {
        this.gameSpeed = value >= 0 ? value : 0;
        cc.director.getScheduler().setTimeScale(this.gameSpeed);
        if (value <= 0) {
            cc.game.pause();
        } else {
            cc.game.resume();
        }
    }

    get IsGamePaused() {
        return this.gameSpeed <= 0;
    }

    get IsNormalGameSpeed() {
        return this.gameSpeed == 1;
    }

    public PauseGame(): void {
        if (this.IsGamePaused) {
            return;
        }
        this.gameSpeedBeforePause = this.GameSpeed;
        this.GameSpeed = 0;
    }

    public ResumeGame(): void {
        if (!this.IsGamePaused) {
            return;
        }
        this.GameSpeed = this.gameSpeedBeforePause;
    }

    public ResetNormalGameSpeed(): void {
        if (this.IsNormalGameSpeed) {
            return;
        }
        this.GameSpeed = 1;

    }


    public ShutDown() {
        this.destroy();
    }

    protected onLoad(): void {

        super.onLoad();
        this.initLogHelper();


    }


    protected update(dt: number): void {
        this._realTime += dt;
        GameFrameworkEntry.Update(dt,this._realTime);
    }

    private initLogHelper() {
        if(this.defaultLogHelperName == undefined ||this.defaultLogHelperName == ""){
            cc.log("defaultLogHelperName is null");
            return;
        }
        let defaultLogHelper = cc.js.getClassByName(this.defaultLogHelperName)
        GameFrameworkLog.SetLogHelper(defaultLogHelper as any);
    }
}